package taskadvanced.core.rtm;

import java.util.Arrays;
import java.util.PriorityQueue;

public class ParameterContainer extends PriorityQueue<Parameter> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1268307259059417451L;
	/**
	 * 
	 */
	//private static final long serialVersionUID = 1268307259059417451L;
	private PriorityQueue<Parameter> container;
	
	public ParameterContainer(){
		container = new PriorityQueue<Parameter>();
	}
	
	public String getQuery(){
		String result = "";
		Object[] parameters = container.toArray();
		Arrays.sort(parameters);
		for (int i=0; i<parameters.length; i++){
			result += ((Parameter)parameters[i]).getKey() + "=" + ((Parameter)parameters[i]).getValue();
			if (i < parameters.length-1)
				result += "&";
		}
		return result;
	}
	
	@Override
	public void clear(){
		container.clear();
	}
	
	@Override
	public boolean add(Parameter newParameter){
		container.remove(newParameter);
		return container.add(newParameter);
	}
	
	@Override
	public String toString(){
		String result = "";
		Object[] parameters = container.toArray();
		Arrays.sort(parameters);
		for (int i=0; i<parameters.length; i++){
			result += parameters[i].toString();
		}
		return result;
	}
	
	
	public void printElem(){
		
		while(!container.isEmpty()){
		    System.out.println(container.poll());		
		}
	
	}
}
